//
//  HEMySignViewController.m
//  HealthFamily
//
//  Created by 吴欧 on 2020/12/10.
//  Copyright © 2020 honglutec. All rights reserved.
//

#import "HEMySignViewController.h"
#import "HEMySignModel.h"
#import "HESignView.h"
#import "HEDakaModel.h"
#import "HEIntegralDetailsViewController.h"
#import "HESignDayModel.h"

@interface HEMySignViewController ()

@property (nonatomic, strong) HEMySignModel *signModel;
@property (nonatomic, strong) UILabel *integralLb;
@property (nonatomic, strong) HEDakaModel *dakaItemModel;
@property (nonatomic, strong) NSMutableArray <HESignDayModel *> *signDayList;
@property (nonatomic, strong) HESignDayModel *currentDay;
@property (nonatomic, strong) UIView *headWarp;

@property (nonatomic, strong) NSMutableArray *dayViewList;
@property (nonatomic, strong) QMUIFillButton *signBtn;

@end

@implementation HEMySignViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"我的康币";
    
    _signDayList = @[].mutableCopy;
    _dayViewList = @[].mutableCopy;
    
    [self setNav];
    [self initUI];
    
    [self loadData];
}

- (void)setNav {
    self.zx_backBtnImageName = @"back_icon";
    self.zx_navBar.backgroundColor = CNavBgColor;
    self.zx_navLineView.hidden = YES;
    self.zx_navTitleColor = KWhiteColor;
    self.zx_navStatusBarStyle = ZXNavStatusBarStyleLight;
}

- (void)initUI {
    UIView *headWarp = [UIView new];
    headWarp.backgroundColor = CNavBgColor;
    [self.view addSubview:headWarp];
    _headWarp = headWarp;
    
    [headWarp mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(ZXNavBarHeight);
        make.right.left.equalTo(self.view);
        make.height.offset(200);
    }];
    
    // 积分
    UILabel *integralLb = [UILabel new];
    integralLb.text = @"-";
    integralLb.textColor = KWhiteColor;
    integralLb.font = DYBlodFont(28);
    [headWarp addSubview:integralLb];
    _integralLb = integralLb;
    
    [integralLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(headWarp).offset(10);
        make.centerX.equalTo(headWarp);
    }];
    
    // 明细
    UILabel *detailslLb = [UILabel new];
    detailslLb.textColor = KWhiteColor;
    detailslLb.text = @"康币明细 >";
    detailslLb.font = DYFont(16);
    [headWarp addSubview:detailslLb];
    detailslLb.userInteractionEnabled = YES;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(toSeeDetailsClick)];
    [detailslLb addGestureRecognizer:tap];
    
    [detailslLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(integralLb.mas_bottom).offset(15);
        make.centerX.equalTo(headWarp);
    }];
     
    // 进度条
    UIProgressView *processView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];
    [headWarp addSubview:processView];
    processView.trackTintColor = [UIColor colorWithHexString:@"#E6CB9C"];
    processView.progressTintColor = [UIColor colorWithHexString:@"#C29355"];
    [processView setProgress:0.4 animated:YES];

    [processView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(detailslLb.mas_bottom).mas_offset(30);
        make.left.equalTo(headWarp.mas_left).mas_offset(15);
        make.right.equalTo(headWarp.mas_right).mas_offset(-15);
        make.height.mas_offset(3);
    }];
    
    // 会员等级
    UILabel *levelLb = [UILabel new];
    [headWarp addSubview:levelLb];
    levelLb.text = [NSString stringWithFormat:@"V%d会员等级    成长值%d/%d",0,0,0];
    levelLb.font = DYBlodFont(16);
    levelLb.numberOfLines = 0;
    levelLb.textColor = KWhiteColor;
    
    [levelLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(processView.mas_bottom).mas_offset(10);
        make.left.equalTo(processView);
        make.right.equalTo(processView);
    }];
}

- (void)initDayUI {
    // 日历
    UIView *dateWarp = [UIView new];
    dateWarp.backgroundColor = KWhiteColor;
    [self.view addSubview:dateWarp];
    
    dateWarp.backgroundColor = [UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1.0];
    dateWarp.layer.shadowColor = [UIColor colorWithRed:13/255.0 green:98/255.0 blue:218/255.0 alpha:0.08].CGColor;
    dateWarp.layer.shadowOffset = CGSizeMake(0,5);
    dateWarp.layer.shadowOpacity = 1;
    dateWarp.layer.shadowRadius = 6;
    dateWarp.layer.cornerRadius = 10;

    [dateWarp mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_headWarp.mas_bottom).mas_offset(-30);
        make.left.equalTo(self.view).mas_offset(10);
        make.right.equalTo(self.view).mas_offset(-10);
        make.height.mas_greaterThanOrEqualTo(195);
    }];
    
    [self dealData];
    
    _dayViewList = [NSMutableArray array];
    for (int i = 0; i < _signDayList.count; i++) {
        HESignDayModel * model = _signDayList[i];
        HESignView *dateView = [HESignView new];
        dateView.model = model;
        [dateWarp addSubview:dateView];
        [_dayViewList addObject: dateView];
    }
    
    CGFloat itemWidth = (KScreenWidth - 20 - 20 - 5 * 6.0) / 7;

    [_dayViewList mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(dateWarp).offset(30);
        make.width.offset(itemWidth);
        make.height.greaterThanOrEqualTo(@70);
    }];

    // 实现masonry水平固定间隔方法
    [_dayViewList mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:5 leadSpacing:10 tailSpacing:10];
    
    // 联系健康管理师
    QMUIFillButton *agreeBtn = [[QMUIFillButton alloc] initWithFillColor:CNavBgColor titleTextColor:CNavBgFontColor];
    agreeBtn.titleLabel.font = DYBlodFont(16);
    [agreeBtn setTitle:@"签到领取康币" forState:UIControlStateNormal];
    [self.view addSubview:agreeBtn];
    
    [agreeBtn addTarget:self action:@selector(toSignDay) forControlEvents:UIControlEventTouchUpInside];
 
    [agreeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(dateWarp.mas_bottom).mas_offset(-20);
        make.centerX.equalTo(dateWarp);
        make.width.mas_offset(220);
        make.height.mas_offset(45);
     }];
    _signBtn = agreeBtn;
}

- (void)dealData {
     
    [self.signDayList removeAllObjects];
    
    NSInteger maxDay = 0;
    if (_dakaItemModel.day >= 5) {
        maxDay = _dakaItemModel.day + 2;
    }else {
        maxDay = 7;
    }
    
    for (NSInteger i = maxDay; i > maxDay - 7; i--) {
        
        HESignDayModel *model = [HESignDayModel new];
        model.day = [NSString stringWithFormat:@"%ld天", i];

        // 设置每天的签到分数
        if (i <= 7) { // 7天内
            HESignItemModel *item = _dakaItemModel.rule[i-1];
            model.pointValue = [NSString stringWithFormat:@"+%@", item.point_value];
            model.score = item.point_value;
        }else {
            model.pointValue = [NSString stringWithFormat:@"+%@", _dakaItemModel.rule.lastObject.point_value];
            model.score = _dakaItemModel.rule.lastObject.point_value;
        }
        
        if (_dakaItemModel.today) { // 设置是不是今天
            if (i == _dakaItemModel.day) {
                model.day = @"今天";
                _currentDay = model;
            }
        }else {
            if (i == _dakaItemModel.day + 1) {
                model.day = @"今天";
                _currentDay = model;
            }
        }
        
        if (i <= _dakaItemModel.day) { // 设置是否签到
            model.isSign = YES;
        }
        
        [self.signDayList addObject:model];
    }
    
    self.signDayList = [[self.signDayList reverseObjectEnumerator] allObjects].mutableCopy;
}

- (void)toSeeDetailsClick {
    HEIntegralDetailsViewController *vc = [HEIntegralDetailsViewController new];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark -

- (void)loadData {
    [WOAFNetWorkingHandle POSTWithBaseUrl:KServerUrl suffixUrl:Klogin_user_point parameters:@{} success:^(id  _Nonnull responseObject) {
        NSLog(@"response：  %@", [responseObject mj_JSONString]);
        HEResponse * response = [HEResponse mj_objectWithKeyValues:responseObject];
        self.signModel = [HEMySignModel mj_objectWithKeyValues:response.data];
        
        [self loadSignItemData];

    } failure:^(NSError * _Nonnull error) {
    }];
}

- (void)getUserAllPoint {
    [WOAFNetWorkingHandle POSTWithBaseUrl:KServerUrl suffixUrl:Klogin_user_point parameters:@{} success:^(id  _Nonnull responseObject) {
        HEResponse * response = [HEResponse mj_objectWithKeyValues:responseObject];
        self.signModel = [HEMySignModel mj_objectWithKeyValues:response.data];
        
        [self refreshData];
    } failure:^(NSError * _Nonnull error) {
    }];
}

- (void)loadSignItemData {
 
    [WOAFNetWorkingHandle POSTWithBaseUrl:KServerUrl suffixUrl:Kdaka_info parameters:@{} success:^(id  _Nonnull responseObject) {
        HEResponse * response = [HEResponse mj_objectWithKeyValues:responseObject];
        self.dakaItemModel = [HEDakaModel mj_objectWithKeyValues:response.data];
         
        [self refreshDayView];
        [self refreshData];

    } failure:^(NSError * _Nonnull error) {
    }];
}

- (void)toSignDay {
  
    [WOAFNetWorkingHandle POSTWithBaseUrl:KServerUrl suffixUrl:KsaveDaKa parameters: @{ @"point" : _currentDay.score } success:^(id  _Nonnull responseObject) {
        HEResponse * response = [HEResponse mj_objectWithKeyValues:responseObject];
        if (response.success) {
            self.currentDay.isSign = YES;
            self.dakaItemModel.today = 1;
            [self refreshDayView];
            [self getUserAllPoint];
        }
    } failure:^(NSError * _Nonnull error) {
    }];
}

- (void)refreshDayView {
    
    if (!(_dayViewList.count > 0)) {
        [self initDayUI];
    }
    
    int i = 0;
    for (HESignView *view in _dayViewList) {
        view.model = _signDayList[i];
        i++;
    }
    
    if (_dakaItemModel.today) {
        _signBtn.userInteractionEnabled = NO;
        [_signBtn setTitle:@"已签到" forState:UIControlStateNormal];
    }else {
        _signBtn.userInteractionEnabled = YES;
        [_signBtn setTitle:@"签到领取康币" forState:UIControlStateNormal];
    }
}

- (void)refreshData {
    _integralLb.text = self.signModel.point;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
